﻿@using AuthoSYS.EF
@model IEnumerable<Permission>
@{
    var items = Model.GroupBy(p => p.ModuleId).ToList();

    foreach (var item in items)
    {
        var permissions = item.ToList();
        var module = Model.FirstOrDefault<Permission>(m => m.ModuleId == item.Key);
    <div style="border: 1px solid #aaa; margin: 10px 0">
        <div class="editor-label">
            @Html.Label(module.Module.ModuleName)
        </div>
        <div class="editor-field">
            @Html.CheckBox("systemModules", false, new { Id = module.Id })
        </div>
        <div style="padding-left: 40px;">
            @foreach (var p in permissions)
            {
                <div class="editor-label">
                    @Html.Label(p.PermissionName)
                </div>
                <div class="editor-field">
                    <input type="checkbox" @(p.IsSelected ? "checked=\"checked\"" : string.Empty) name="permissionValues" id="@(p.Id)" value="@(p.PermissionValue)" moduleId="@(module.Id)" />
                </div>
            }
        </div>
    </div>
    }
     
}
<script type="text/javascript">
    $(document).ready(function () {
        $("input[type=checkbox][name='systemModules']").live("click", function () {
            var id = $(this).attr('id');
            var isChecked = $(this).attr('checked');
            $("input[type=checkbox][moduleId='" + id + "']").each(function () {
                if (isChecked != undefined) {
                    $(this).attr('checked', true);
                } else {
                    $(this).removeAttr('checked');
                }

            });
        });
    });
</script>
